package com.zw.stock_sell_storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.stock_sell_storage.dto.MyPageDto;
import com.zw.stock_sell_storage.dto.PurchasingDetailDto;
import com.zw.stock_sell_storage.dto.PurchasingInformationDto;
import com.zw.stock_sell_storage.dto.ex.PurchasingInformationDtoEx;
import com.zw.stock_sell_storage.dto.ex.QueryTimeScopeDtoEx;
import com.zw.stock_sell_storage.mapper.PurchasingInformationMapper;
import com.zw.stock_sell_storage.service.IPurchasingDetailService;
import com.zw.stock_sell_storage.service.IPurchasingInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PurchasingInformationServiceImpl extends ServiceImpl<PurchasingInformationMapper, PurchasingInformationDto> implements IPurchasingInformationService {
    @Autowired
    PurchasingInformationMapper purchasingInformationMapper;
    @Autowired
    IPurchasingDetailService iPurchasingDetailService;
    @Override
    public Page<PurchasingInformationDto> queryPurchasingInformationList(MyPageDto<QueryTimeScopeDtoEx<PurchasingInformationDto>> myPageDto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (myPageDto.getData().getData().getPurchasingSuppliersName()!=null&&!"".equals(myPageDto.getData().getData().getPurchasingSuppliersName())){
            queryWrapper.like("purchasing_suppliers_name",myPageDto.getData().getData().getPurchasingSuppliersName());
        }
        if (myPageDto.getData().getStartTime()!=null&&!"".equals(myPageDto.getData().getStartTime())){
            queryWrapper.ge("purchasing_time",myPageDto.getData().getStartTime());
        }
        if (myPageDto.getData().getEndTime()!=null&&!"".equals(myPageDto.getData().getEndTime())){
            queryWrapper.le("purchasing_time",myPageDto.getData().getEndTime());
        }
        return purchasingInformationMapper.selectPage(new Page<>(myPageDto.getCurrentPage(),myPageDto.getPageSize()),queryWrapper);
    }

    @Override
    public PurchasingInformationDtoEx queryPurchasingInformationById(int purchasingId) {
        PurchasingInformationDto purchasingInformationDto = purchasingInformationMapper.selectById(purchasingId);
        List<PurchasingDetailDto> purchasingDetailList = iPurchasingDetailService.queryPurchasingDetailByPurchasingId(purchasingId);
        PurchasingInformationDtoEx purchasingInformationDtoEx = new PurchasingInformationDtoEx();
        purchasingInformationDtoEx.setPurchasingId(purchasingInformationDto.getPurchasingId());
        purchasingInformationDtoEx.setPurchasingSuppliersName(purchasingInformationDto.getPurchasingSuppliersName());
        purchasingInformationDtoEx.setPurchasingAgent(purchasingInformationDto.getPurchasingAgent());
        purchasingInformationDtoEx.setPurchasingTime(purchasingInformationDto.getPurchasingTime());
        purchasingInformationDtoEx.setPurchasingDetailList(purchasingDetailList);
        return purchasingInformationDtoEx;
    }

    @Override
    @Transactional
    public boolean addPurchasingInformation(PurchasingInformationDto purchasingInformationDto) {
        purchasingInformationMapper.insert(purchasingInformationDto);
        return true;
    }
}
